﻿package frEngine.shader.filters.vertexFilters
{
    import baseEngine.system.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.filters.vertexFilters.def.*;

    public class ShadowVertexFilter extends VertexFilter
    {
        private var angle:Number = -0.785398;

        public function ShadowVertexFilter()
        {
            super(FilterType.Transform);
            return;
        }// end function

        override public function setRegisterParams(param1:Program3dRegisterInstance) : void
        {
            return;
        }// end function

        override public function get programeId() : String
        {
            return "ShadowVertexFilter";
        }// end function

        override public function clone() : FilterBase
        {
            return new ShadowVertexFilter();
        }// end function

        public function set skinNum(param1:int) : void
        {
            if (param1 < 0)
            {
                throw new Error("curMaxBoneNum can not lower than 0 !");
            }
            return;
        }// end function

        override public function createVertexCode(param1:Program3dRegisterInstance, param2:int) : String
        {
            var _loc_3:String = "";
            if (param2 > 0)
            {
                _loc_3 = this.createSkinTransform(param1, param2);
            }
            else
            {
                _loc_3 = this.createNoSkinTransform(param1);
            }
            param1.OpType = ECalculateOpType.ViewProj;
            return _loc_3;
        }// end function

        private function createNoSkinTransform(param1:Program3dRegisterInstance) : String
        {
            var _loc_2:* = Math.sin(this.angle);
            var _loc_3:* = Math.cos(this.angle);
            var _loc_4:Array = [];
            _loc_4.push(new ToBuilderInfo("va1", FilterName.POSITION, false, 3));
            _loc_4.push(new ToBuilderInfo("vm0", FilterName.global, true, 4));
            _loc_4.push(new ToBuilderInfo("vc1", "{shadowParam}", false, 1, [_loc_2, 0, _loc_3, 0.75]));
            _loc_4.push(new ToBuilderInfo("vf0", "{V_GlobleHeight}", false, 1));
            var _loc_5:String = "";
            _loc_5 = _loc_5 + "m44          vt0    \t  \t\t  va1         vm0   \t\t \t    \n";
            _loc_5 = _loc_5 + "mov         {V_GlobleHeight} vt0.y           \t\t\t\t\t\n";
            _loc_5 = _loc_5 + "mul           vt1.w          vt0.y       \tvc1.w  \t\t\t\t\n";
            _loc_5 = _loc_5 + "mul           vt1.xyz        vc1.xyz     \tvt1.w  \t\t\t\t\n";
            _loc_5 = _loc_5 + "add           vt0.xyz        vt0.xyz      vt1.xyz  \t\t\t\n";
            _loc_5 = _loc_5 + "mov           vt0.y         {vcConst1}.x  \t\t\t\t\t\n";
            _loc_5 = _loc_5 + "mov         \t {output}      vt0           \t\t\t\t\t\n";
            _loc_5 = param1.toBuild(_loc_5, _loc_4);
            return _loc_5;
        }// end function

        private function createSkinTransform(param1:Program3dRegisterInstance, param2:int) : String
        {
            var _loc_3:* = Math.sin(this.angle);
            var _loc_4:* = Math.cos(this.angle);
            var _loc_5:Array = [new ToBuilderInfo("vc1-105", "{bones}", false, Device3D.maxBonesPerSurface * 3 + 1, []), new ToBuilderInfo("vm0", FilterName.global, true, 4), new ToBuilderInfo("vc1", "{shadowParam}", false, 1, [_loc_3, 0, _loc_4, 0.75]), new ToBuilderInfo("va1", FilterName.SKIN_INDICES, false, param2), new ToBuilderInfo("va2", FilterName.SKIN_WEIGHTS, false, param2), new ToBuilderInfo("vf0", "{V_GlobleHeight}", false, 1), new ToBuilderInfo("va3", FilterName.POSITION, false, 3)];
            var _loc_6:String = "mov           vt0           vc0 \t\t\t\t\t\t\t\t\t\n";
            switch(param2)
            {
                case 1:
                {
                    _loc_6 = _loc_6 + ("m34           vt0.xyz       va3         \t vc[va1.x]   \t\t\n" + "mul           vt0       \tvt0           va2.x         \t\t\n");
                    break;
                }
                case 2:
                {
                    _loc_6 = _loc_6 + ("m34           vt0.xyz       va3         \t vc[va1.x]   \t\t\n" + "mul           vt2       \t vt0           va2.x         \t\t\n" + "m34           vt0.xyz       va3         \t vc[va1.y]   \t\t\n" + "mul           vt3           vt0           va2.y         \t\t\n" + "add           vt0.xyz       vt2           vt3           \t\t\n");
                    break;
                }
                case 3:
                {
                    _loc_6 = _loc_6 + ("m34           vt0.xyz       va3         \t vc[va1.x]   \t\t\n" + "mul           vt2       \t vt0           va2.x         \t\t\n" + "m34           vt0.xyz       va3         \t vc[va1.y]   \t\t\n" + "mul           vt3           vt0           va2.y         \t\t\n" + "m34           vt0.xyz       va3         \t vc[va1.z]   \t\t\n" + "mul           vt4           vt0           va2.z         \t\t\n" + "add           vt0.xyz       vt2           vt3           \t\t\n" + "add           vt0.xyz       vt4           vt0           \t\t\n");
                    break;
                }
                case 4:
                {
                    _loc_6 = _loc_6 + ("m34           vt0.xyz       va3         \t vc[va1.x]   \t\t\n" + "mul           vt2       \t vt0           va2.x         \t\n" + "m34           vt0.xyz       va3         \t vc[va1.y]   \t\t\n" + "mul           vt3           vt0           va2.y         \t\n" + "m34           vt0.xyz       va3         \t vc[va1.z]   \t\t\n" + "mul           vt4           vt0           va2.z         \t\n" + "m34           vt0.xyz       va3         \t vc[va1.w]   \t\t\n" + "mul           vt5           vt0           va2.w         \t\t\n" + "add           vt0.xyz       vt2           vt3           \t\t\n" + "add           vt0.xyz       vt4           vt0           \t\t\n" + "add           vt0.xyz       vt5           vt0           \t\t\n");
                    break;
                }
                default:
                {
                    break;
                }
            }
            _loc_6 = _loc_6 + "mov           vt0.w          {vcConst1}.z         \t\t\t\n";
            _loc_6 = _loc_6 + "m44           vt0         \t  vt0\t\t\tvm0\t\t\t\t\t\n";
            _loc_6 = _loc_6 + "mov         {V_GlobleHeight} vt0.y           \t\t\t\t\t\n";
            _loc_6 = _loc_6 + "mul           vt1.w          vt0.y       \tvc1.w  \t\t\t\t\n";
            _loc_6 = _loc_6 + "mul           vt1.xyz        vc1.xyz     \tvt1.w  \t\t\t\t\n";
            _loc_6 = _loc_6 + "add           vt0.xyz        vt0.xyz      vt1.xyz  \t\t\t\n";
            _loc_6 = _loc_6 + "mov           vt0.y         {vcConst1}.x  \t\t\t\t\t\n";
            _loc_6 = _loc_6 + "mov         \t {output}      vt0           \t\t\t\t\t\n";
            _loc_6 = param1.toBuild(_loc_6, _loc_5);
            return _loc_6;
        }// end function

        override public function createFragmentUvCoord(param1:Program3dRegisterInstance) : String
        {
            var _loc_2:* = new Array();
            var _loc_3:String = "";
            _loc_3 = _loc_3 + "sub\t\t\tft0.x\t   {V_GlobleHeight}.y\t\t{fcConst1}.x\t\n";
            _loc_3 = _loc_3 + "kil\t\t\tft0.x\t   \t\t\t\t\t\t\t\t\t\t\t\n";
            _loc_3 = param1.toBuild(_loc_3, _loc_2);
            return _loc_3;
        }// end function

    }
}
